# Created by 黄景涛
# DATE: 2024/10/14
from django.http import HttpResponse


def set_csp(policy):
    """
    响应头，设置csp关键字
    :param policy: 内容策略值
    :return: view_func
    """
    def decorator(view_func):
        def _wrapper(request, *args, **kwargs):
            response = view_func(request, *args, **kwargs)

            # 判断response是否为HttpResponse实例
            if not isinstance(response, HttpResponse):
                response = HttpResponse(response)

            response["Content-Security-Policy"] = policy
            return response
        return _wrapper
    return decorator
